package com.ujs.thymeleaf;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
public class LoginController {


    @RequestMapping("/login")
    public String login(Model model ){/*, @RequestParam Product product)*/
        return "login";
    }

    @GetMapping("/failer")
    public String failer(Model model ){/*, @RequestParam Product product)*/
        return "failer";
    }

    //springsecurity，在页面中显示用户信息
    @GetMapping("/main")
    public String main(HttpSession session){
        //获取用户信息，存放到session域中
        SecurityContextImpl securityContextImpl =(SecurityContextImpl)session.getAttribute("SPRING_SECURITY_CONTEXT");
        Authentication authentication = securityContextImpl.getAuthentication();
        //将主体转为用户详情对象
        UserDetails userDetails =(UserDetails)authentication.getPrincipal();
        String username = userDetails.getUsername();
        session.setAttribute("username",username);
        return "index";
    }

}
